﻿@model Mulala.Cavan.Presentation.Mvc.ViewModel.ICorporationPhotosViewModel
@{
    ViewBag.Title = "photo";
    Layout = "~/Views/Shared/_CorporationCenterLayout.cshtml";
}
@section header
{
    <title>@ViewBag.Title</title>
    @Scripts.Render("~/bundles/fileUpload")
}
@{
    Html.RenderAction("recommendconsign", "Consign");
}
@{
    Html.RenderAction("popularproprietor", "Proprietor");
}
@{
    int index = 1;
    string fileID = string.Empty;
    string photoID = string.Empty;
    string saveID = string.Empty;
    string corporationPhotoID = string.Empty;
    
}
@foreach (ICorporationPhotoViewModel cp in @Model.Photos)
{
    fileID = "file" + index.ToString();
    photoID = "photo" + index.ToString();
    saveID = "save" + index.ToString();
    corporationPhotoID = "corporationPhoto" + index.ToString();
    <input type="file" id="@fileID" name="file" onchange="return ajaxFileUpload(@index);"/>
    <img id="@photoID" src="@cp.Photo" alt="" />
    <input type="button" id="@saveID" value="保存"/>
    <input type="hidden" id="@corporationPhotoID" value="@cp.PhotoID"/>
    index = index + 1;
}
@{
    for (int currentIndex = index; currentIndex < @Model.RestrictFileCount; currentIndex++)
    {
        fileID = "file" + index.ToString();
        photoID = "photo" + index.ToString();
        saveID = "save" + index.ToString();
        corporationPhotoID = "corporationPhoto" + index.ToString();
    <input type="file" id="@fileID" name="file" onchange="return ajaxFileUpload(@index);"/>
    <img id="@photoID" src="" alt="" />
    <input type="button" id="@saveID" value="保存"/>
    <input type="hidden" id="@corporationPhotoID" value="0"/>
        index = index + 1;
    }
}
<input type="hidden" id="attachmentGroup" value="@Model.AttachmentGroup.GetHashCode().ToString()"/>
<script type="text/javascript">
    $(document).ready(function () {
        $(":button").bind("click", function () {
            var index = $(this).attr("id").substring(4);
            var photoModel = getPhotoModel(index);
            if (photoModel == null) {
                return;
            }
            var json = $.toJSON(photoModel);
            $.ajax({
                url: '/corporationcenter/photo',
                type: 'POST',
                dataType: 'json',
                data: json,
                contentType: 'application/json; charset=utf-8',
                success: function (data) {
                    if (data) {
                        if (data.IsSuccess) {
                            alert(data.Messages[0]);
                        }
                    }
                },
                error: function () {
                }
            })
        });

        function getPhotoModel(index) {
            var photoUrl = $("#photo" + index)[0].src;
            var photoId = $("#corporationPhoto" + index).val();

            return (photoUrl == "") ? null : { PhotoID: photoId, Photo: photoUrl };
        }
    });

    function ajaxFileUpload(index) {
        var attachmentGroup = $("#attachmentGroup").val();
        $.ajaxFileUpload
        (
            {
                url: '/attachment/upload/' + attachmentGroup, //用于文件上传的服务器端请求地址
                secureuri: false, //一般设置为false
                fileElementId: 'file' + index, //文件上传空间的id属性  <input type="file" id="file" name="file" />
                dataType: 'json', //返回值类型 一般设置为json
                success: function (data, status)  //服务器成功响应处理函数
                {
                    $("#photo" + index).attr("src", data.Messages[0] + "_360×360.jpg");
                },
                error: function (data, status, e)//服务器响应失败处理函数
                {
                    alert(e);
                }
            }
        )

        return false;
    }
</script>
